<?php
namespace app\index\controller;
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/29
 * Time: 13:26
 */
use think\config\driver\Json;
use think\Controller;
use \think\Db;
use \think\Session;
class Material extends Controller{
    function index(){
        $user=Session::get('user');
        $this->assign('user_level',$user->level);
        return $this->fetch('manageMaterial');
    }

    //获得教材列表信息
    function getBooks(){

        $page = input('page');
        $limit = input('limit');


        $start = ($page-1) * $limit;

        $result['code'] = 1;
        $result['count'] = 0;

        $result['data'] = Db::table('books')
            ->limit($start,$limit)
            ->field('book_id,book_name,book_company,book_word_count,status')
            ->select();
        $result['count'] = count(Db::table('books')->select());
        if(!empty($result['data'])){
            $result['msg'] = '成功获得数据';
        }
        else{
            $result['code'] = 0;
            $result['msg'] = '无数据';
        }
        return \json($result);
    }

    //获得一本教材的详细信息
    function getBookInfo(){

        $book_id = input('book_id');

        $result['code'] = 1;

        $result['authors'] = Db::table('books_author')
            ->alias('a')
            ->where('a.book_id','=',$book_id)
            ->join('author c','c.author_id = a.author_id')
            ->field('author_name,level,c.author_id')
            ->select();
        $result['book'] = Db::table('books')
            ->where('book_id','=',$book_id)
            ->find();
        if(!empty($result['authors'])&&!empty($result['book'])){
            $result['msg'] = '成功获得数据';
        }
        else{
//            $result['code'] = 0;
            $result['msg'] = '无数据';
        }

        $arr_authors = array('1'=>'','2'=>'','3'=>'','4'=>'','5'=>'','6'=>'','7'=>'','8'=>'','9'=>'','11'=>'');
        foreach ($result['authors'] as $ret){
                $arr_authors[$ret['level']] = $ret['author_name'];
        }

        $result['others'] = Db::table('books_author')
            ->alias('a')
            ->where('a.book_id','=',$book_id)
            ->where('level','=',10)
            ->join('author c','c.author_id = a.author_id')
            ->field('author_name')
            ->select();
        $result['board'] = Db::table('books_author')
            ->alias('a')
            ->where('a.book_id','=',$book_id)
            ->where('level','=',11)
            ->join('author c','c.author_id = a.author_id')
            ->field('author_name')
            ->select();

        $this->assign('authors',$arr_authors);
        $this->assign('book_name',$result['book']['book_name']);
        $this->assign('book_company',$result['book']['book_company']);
        $this->assign('book_out_datetime',$result['book']['book_out_datetime']);
        $this->assign('book_id',$result['book']['book_id']);
        $this->assign('status',$result['book']['status']);
        $this->assign('book_word_count',$result['book']['book_word_count']);

        $user=Session::get('user');
        $this->assign('user_level',$user->level);
        $this->assign('others', $result['others']);
        $this->assign('board', $result['board']);
        return $this->fetch('manageAuthor');
//        return \json( $result);
    }

    //获取教材其他副主编或者编委信息接口
    public function getOthers(){

        $book_id = input('book_id');
        $level = input('level');

        $ret = Db::table('books_author')
            ->alias('a')
            ->where('a.book_id','=',$book_id)
            ->where('level','=',$level)
            ->join('author c','c.author_id = a.author_id')
            ->field('author_name,c.author_id')
            ->select();
        if($ret){
            $this->success('获得其他副主编信息成功','',$ret);
//            return \json($ret);
        }
        else{
            $this->error('无数据');
        }
    }

    //插入教材信息
    public function addBook(){


        $data['book_name'] = input('book_name');
        $data['book_company'] = input('book_company');
        $data['book_out_datetime'] = input('book_out_datetime');
        $data['book_word_count'] = input('book_word_count');
        $data['status'] = input('status');

        $isAdd = Db::table('books')->where('book_name','=',$data['book_name'])->select();

        if($isAdd){
            $this->error('重复添加');
        }

        $add = Db::table('books')->insert($data);

        if($add){
            $this->success('添加成功');
        }
        else{
            $this->error('添加失败');
        }

    }

    //修改教材信息
    public function updateBook(){

        $book_id = input('book_id');
        $data['book_name'] = input('book_name');
        $data['book_company'] = input('book_company');
        $data['book_out_datetime'] = input('book_out_datetime');
        $data['book_word_count'] = input('book_word_count');
        $data['status'] = input('status');


        $ret = Db::table('books')->where('book_id','=',$book_id)->update($data);

        if($ret){
            $this->success('修改成功');
        }
        else{
            $this->error('修改失败');
        }

    }

    //修改主编信息
    public function updateEditor(){

        $data['author_name'] = input('author_name');
        $data['author_work_place'] = input('author_work_place');
        $data['author_introduction'] = input('author_introduction');
        $data['author_pic'] = input('author_pic');


        $ret = Db::table('author')->where('author_id','=',input('author_id'))->update($data);

        if($ret){
            $this->success('修改成功');
        }
        else{
            $this->error('修改失败');
        }

    }
    //修改副主编信息
    public function updateSubEditor(){

        $data['author_name'] = input('author_name');
        $data['author_work_place'] = input('author_work_place');

        $ret = Db::table('author')->where('author_id','=',input('author_id'))->update($data);

        if($ret){
            $this->success('修改成功');
        }
        else{
            $this->error('修改失败');
        }

    }

    //删除教材信息
    public function delBook(){

        $data['book_id'] = input('book_id');

        $res = Db::table('books')->delete($data['book_id']);

        if($res){
            $this->success('删除成功');
        }
        else{
            $this->error('删除失败');
        }

    }

    //添加教材界面
    public function insertMaterial(){
        return view();
    }

    //修改教材信息返回界面
    public function changeMaterial(){
        $book_id = input('book_id');

        $result['book'] = Db::table('books')
            ->where('book_id','=',$book_id)
            ->find();

        $this->assign('book_name',$result['book']['book_name']);
        $this->assign('book_word_count',$result['book']['book_word_count']);
        $this->assign('book_company',$result['book']['book_company']);
        $this->assign('status',$result['book']['status']);
        $this->assign('book_out_datetime',$result['book']['book_out_datetime']);
        $this->assign('book_id',$result['book']['book_id']);

        return view();
    }

    public function manageOthers(){
        $book_id = input('book_id');
        $author_level = input('author_level');
        $this->assign('book_id',$book_id);
        $this->assign('author_level',$author_level);
        return view();
    }

    public function changeOthers(){
        $book_id = input('book_id');
        $author_level = input('level');
        $this->assign('book_id',$book_id);
        $this->assign('author_level',$author_level);
        return view();
    }
}